<?php

require_once ROOT_DIR.'models/User.php';

/**
 * Tries to create a new user with the given data and prints the view of the results.
 * @param string $loginName
 * @param string $firstName
 * @param string $lastName
 * @param string $password
 * @param string $email
 */
function newUser($loginName, $password, $firstName, $lastName, $email){
	//check if login name is already in use
	if(is_null(User::getUserByName($loginName))){
		$user = new User($loginName, md5($password), $firstName, $lastName, $email);
		if($user->saveToDB()){
			$res = new ResultJSON(false, session_id());
			$res->addItem('uID', $user->getID());
			echo $res->renderView();
		} else {
			$res = new ResultJSON(true, session_id(), ERROR_ACTION_FAILED);
			echo $res->renderView();
		}
	} else {
		$res = new ResultJSON(true, session_id(), ERROR_USER_NAME_ALREADY_IN_USE);
		echo $res->renderView();
	}
}

?>